// 这份代码专门处理逻辑层的操作
const bookDao = require('../dao/bookDao');
const powerDao = require('../dao/powerDao');

const bookService = {
    /*
     * 获取推荐书籍接口
     */
    async getCommendBook(uid, lastId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看参考书');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let bookList = await bookDao.getCommendBook(uid, lastId);
            result = { success: true, msg: '获取推荐书籍成功', data: { bookList } };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 加入书架接口
     */
    async joinInLib(uid, bookId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看参考书');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            await bookDao.joinInLib(uid, bookId);
            result = { success: true, msg: '获取加入书架成功' };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 取消加入书架接口
     */
    async noJoinInLib(uid, bookId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看参考书');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            await bookDao.noJoinInLib(uid, bookId);
            result = { success: true, msg: '取消加入书架成功' };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 懒加载获取书架中书籍接口
     */
    async getJoinBook(uid, lastId) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看参考书');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let bookList = await bookDao.getJoinBook(uid, lastId);
            result = { success: true, msg: '加载书架成功', data: { bookList } };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    /*
     * 获取书本内容接口
     */
    async getBookContent(bookId, uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看参考书');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await bookDao.getBookContent(bookId);
            if (data.length) {
                result = { success: true, msg: '加载书本内容成功', data: data[0] };
            } else {
                result = { success: false, msg: '书本内容不存在' };
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    async getAllBook(uid, page) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '书籍管理');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await bookDao.getAllBook(page);
            result = { success: true, msg: '加载所有书本成功', data };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    },
    async searchBook(uid, bookName) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看参考书');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await bookDao.searchBook(uid, bookName);
            result = { success: true, msg: '搜索书本成功', data };
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
        }
        return result;
    }
};

module.exports = bookService;